﻿@model List<HZYProject.Domain.Course>
@{ var list = ViewBag.Names as List<HZYProject.Domain.TestPaper>;}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">考试名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">所属课程</label>
                <div class="layui-input-inline">
                    <select id="CourseID" name="CourseID" lay-search="">
                        <option value="">--全部--</option>
                        @{
                            foreach (var item in Model)
                            {
                                <option value="@item.CourseID">@item.Title</option>
                            }
                        }
                    </select>
                </div>
                <label class="layui-form-label">所属试卷</label>
                <div class="layui-input-inline">
                    <select id="TestPaperID" name="TestPaperID" lay-search="">
                        <option value="">--全部--</option>
                        @{
                            foreach (var item in list)
                            {
                                <option value="@item.TestPaperID">@item.Title</option>
                            }
                        }
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    @*<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="setStu"><i class="layui-icon layui-icon-set"></i></button>*@
    <button class="pear-btn pear-btn-sm" id="more_{{d.examID}}"><i class="layui-icon layui-icon-triangle-d"></i></button>

</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script type="text/html" id="user-startTime">
    {{layui.util.toDateString(d.startTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>

<script type="text/html" id="user-course">
    {{d.course.title}}
</script>

<script type="text/html" id="user-testPaper">
    {{d.testPaper.title}}
</script>
@section script{
    <script>
        layui.use(['table', 'form', 'jquery', 'dropdown'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let dropdown = layui.dropdown;

            let MODULE_PATH = "/Exam";

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '序号',
                    type: "numbers",
                    align: 'left'
                },
                {
                    title: '考试编号',
                    field: 'examID',
                    align: 'left'
                },
                {
                    title: '课程编号',
                    field: 'courseID',
                    align: 'left'
                },
                {
                    title: '试卷编号',
                    field: 'testPaperID',
                    align: 'left'
                },
                {
                    title: '考试名称',
                    field: 'title',
                    align: 'left'
                },
                {
                    title: '所属课程',
                    field: 'courseName',
                    align: 'left'
                },
                {
                    title: '所属试卷',
                    field: 'testPaperTitle',
                    align: 'left'
                },
                {
                    title: '考试时长',
                    field: 'duration',
                    align: 'left'
                },
                {
                    title: '开始时间',
                    field: 'startTime',
                    align: 'left',
                    width: 200,
                    templet: '#user-startTime'
                },
                {
                    title: '人数',
                    field: 'stuNumber',
                    align: 'left'
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'left',
                    templet: '#user-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'left',
                    width: 160,
                    fixed: 'right'
                }]
            ]

            table.render({
                elem: '#user-table',
                url: '/Exam',
                method: 'post',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
                , done: function (res, curr, count) {
                    $("[data-field='examID']").css('display', 'none');
                    $("[data-field='courseID']").css('display', 'none');
                    $("[data-field='testPaperID']").css('display', 'none');
                    for (var i = 0; i < res.data.length; i++) {
                        dropdown.render({
                            elem: '#more_' + res.data[i].examID
                            , data: [{
                                title: '添加考试人员'
                                , examID: res.data[i].examID
                                , id: 100
                            }, {
                                title: '查看考试人员'
                                , examID: res.data[i].examID
                                , id: 101
                            }]
                            , id: '#more_' + res.data[i].examID
                            , click: function (obj) {
                                if (obj.id == 100)
                                    window.setStu(obj);
                                else if (obj.id == 101)
                                    window.examPerson(obj);
                            }
                        });
                    }
                }
            });

            table.on('tool(user-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'setStu') {
                    window.setStu(obj);
                } else if (obj.event === 'examPerson') {
                    window.examPerson(obj);
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            form.on('submit(user-query)', function (data) {
                table.reload('user-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['400px', '500px'],
                    content: MODULE_PATH + '/add'
                });
            }

            window.edit = function (obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['400px', '500px'],
                    content: MODULE_PATH + '/edit?id=' + obj.data.examID
                });
            }

            window.setStu = function (obj) {
                layer.open({
                    type: 2,
                    title: '添加考试人员',
                    shadeClose: true,
                    shade: 0.1,
                    maxmin: true,
                    anim: 1,
                    area: ['500px', '400px'],
                    content: MODULE_PATH + '/setStu?id=' + obj.examID
                });
            }
            window.examPerson = function (obj) {
                layer.open({
                    type: 2,
                    title: '查看考试人员',
                    shadeClose: true,
                    shade: 0.1,
                    maxmin: true,
                    anim: 1,
                    area: ['500px', '400px'],
                    content: MODULE_PATH + '/examPerson?id=' + obj.examID
                });
            }

            window.remove = function (obj) {
                layer.confirm('确定要删除该章节吗？如删除，将删除和章节相关的所有数据。请谨慎操作。', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/remove/" + obj.data['examID'],
                        method: 'post',
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                    window.location.reload();
                                });
                            } else {
                                layer.msg("删除失败：" + result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('user-table');
            }

        })
    </script>
}
